# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Nikita Volodin <volodin.n at gmail dot com>

pkgname=keycloak
pkgver=22.0.5
_java=17
pkgrel=1
pkgdesc="Open Source Identity and Access Management For Modern Applications and Services"
arch=('any')
url="https://www.keycloak.org/"
license=('Apache')
depends=("java-runtime-headless=${_java}" 'grep' 'bash' 'coreutils' 'util-linux')
makedepends=('maven' "java-environment=${_java}")
backup=(
  'etc/keycloak/keycloak.conf'
)
install=keycloak.install
options=(emptydirs)
source=(https://github.com/keycloak/keycloak/archive/$pkgver/$pkgname-$pkgver.tar.gz
        keycloak.service
        keycloak.sysusers
        keycloak.tmpfiles
        common.sh
        pin-java-version.patch
        )
sha512sums=('e61ff4d8bc116396c67eeea89da923bbd63e88f59e448293782f3434d10d477609998d2293fb6b29711c6f48a8039a130649e936e6d182b31bf26cf17d1f0150'
            '925ca021a9989a6d5181a90f42ec9e67a4957e98bb716acba75e13e1b01a03e4bd3a5939f5f0abe4cf57157be54dda5e373286041b22d84f5079eba94df4e6c9'
            '2e2ba147007ad74e38579a8838d79de47beac509b4bd1a14d7f80905953d79a7396d781f141b461ec688f5ceef9a1081a825a4ca8afc1ea12c178d8ae7f5a7dd'
            '155db40105c08d0aaa810ca5533dc16fc9f82060280541ede6fafd754d30b4844f6d10ace1417a5ad68d89fc54e1b9e6d906ce7ccf973f4ac964422211ed9a72'
            '27c2b0d8ef2f64a515410e5316524eed879d67c42a695642c23653f59bbdc37ff7aaff3414fa6779e97ae477c51014aadb2f0256effdf7abf65f1778fe8885bd'
            '38f014de00db7ec1b5693529401f316f70474c97484cfdea0a7048da6bb9d1ace8fdc915bf8857edcd6aae0d0dd9c1e0b2c0be0aaa99d105be09bed975f049fd')

prepare() {
  cd $pkgname-$pkgver

  patch -Np1 -i "$srcdir"/pin-java-version.patch
}

build() {
  cd $pkgname-$pkgver

  export PATH="/usr/lib/jvm/java-${_java}-openjdk/bin:$PATH"
  mvn -am \
    -s maven-settings.xml \
    -DskipTestsuite \
    -DskipExamples \
    -DskipTests \
    -Dproject.build.outputTimestamp="$SOURCE_DATE_EPOCH" \
    clean install
}

package() {
  cd $pkgname-$pkgver

  install -vdm 755 "${pkgdir}"/{usr/share/java,var/log}/"${pkgname}"
  install -vdm 755 "${pkgdir}"/var/lib/"${pkgname}"/{deployments,data}
  install -vdm 755 "${pkgdir}"/usr/bin

  tar xf quarkus/dist/target/${pkgname}-${pkgver}.tar.gz --strip 1 \
    -C "${pkgdir}/usr/share/java/${pkgname}"
  install -vDm 755 "${srcdir}/common.sh" "${pkgdir}/usr/share/java/${pkgname}/bin/common.sh"
  # Clean up unwanted files
  rm -rvf "${pkgdir}/usr/share/java/${pkgname}"/LICENSE.txt
  rm -rvf "${pkgdir}/usr/share/java/${pkgname}"/bin/*.bat
  # Fix permissions from untar
  chown -R root:root "${pkgdir}/usr/share/java/${pkgname}"

  install -vdm 755 "${pkgdir}/etc"
  mv -v "${pkgdir}/usr/share/java/${pkgname}/conf" "${pkgdir}/etc/${pkgname}"

  ln -svf /var/log/keycloak "${pkgdir}/usr/share/java/${pkgname}/log"
  ln -svf /var/lib/keycloak/deployments "${pkgdir}/usr/share/java/${pkgname}/deployments"
  ln -svf /var/lib/keycloak/data "${pkgdir}/usr/share/java/${pkgname}/data"

  install -vDm 644 "${srcdir}"/keycloak.service "${pkgdir}"/usr/lib/systemd/system/keycloak.service
  install -vDm 644 "${srcdir}"/keycloak.sysusers "${pkgdir}"/usr/lib/sysusers.d/keycloak.conf
  install -vDm 644 "${srcdir}"/keycloak.tmpfiles "${pkgdir}"/usr/lib/tmpfiles.d/keycloak.conf

  ln -svf /usr/share/java/keycloak/bin/kc.sh "${pkgdir}"/usr/bin/kc.sh
  ln -svf /usr/share/java/keycloak/bin/kcadm.sh "${pkgdir}"/usr/bin/kcadm.sh
  ln -svf /usr/share/java/keycloak/bin/kcreg.sh "${pkgdir}"/usr/bin/kcreg.sh

  install -Dm 644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE.txt"
}

# vim: ts=2 sw=2 et:
